


package ciicos.check.controller;

import ciicos.base.ActionResult;
import ciicos.base.UserInfo;
import ciicos.base.vo.PageListVO;
import ciicos.base.vo.PaginationVO;
import ciicos.check.entity.CheckTaskAnomalyEntity;
import ciicos.check.entity.CheckTaskEntity;
import ciicos.check.entity.CheckTaskItemEntity;
import ciicos.check.model.checktaskanomaly.CheckTaskAnomalyForm;
import ciicos.check.model.checktaskanomaly.CheckTaskAnomalyInfoVO;
import ciicos.check.model.checktaskanomaly.CheckTaskAnomalyListVO;
import ciicos.check.model.checktaskanomaly.CheckTaskAnomalyPagination;
import ciicos.check.service.CheckTaskAnomalyService;
import ciicos.check.service.CheckTaskItemService;
import ciicos.check.service.CheckTaskService;
import ciicos.exception.DataException;
import ciicos.equ.service.EquBaseService;
import ciicos.util.GeneraterSwapUtil;
import ciicos.util.JsonUtil;
import ciicos.util.StringUtil;
import ciicos.util.UserProvider;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 巡检异常
 *
 * @版本： V3.2.0
 * @版权：
 * @作者： 产研项目租
 * @日期： 2023-08-01
 */
@Slf4j
@RestController
@Api(tags = "巡检异常", value = "checkManage")
@RequestMapping("/api/checkManage/CheckTaskAnomaly")
public class CheckTaskAnomalyController {

    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;


    @Autowired
    private UserProvider userProvider;


    @Autowired
    private CheckTaskAnomalyService checkTaskAnomalyService;

    @Autowired
    private CheckTaskService checkTaskService;

    @Autowired
    private CheckTaskItemService checkTaskItemService;

    @Autowired
    private EquBaseService equBaseService;
    /**
     * 列表
     *
     * @param checkTaskAnomalyPagination
     * @return
     */
    @ApiOperation("获取列表")
    @PostMapping("/getList")
    public ActionResult list(@RequestBody CheckTaskAnomalyPagination checkTaskAnomalyPagination) throws IOException {
        //树形判断是否有参数，有参数不造树
        List<CheckTaskAnomalyEntity> list = checkTaskAnomalyService.getList(checkTaskAnomalyPagination);
        //处理id字段转名称，若无需转或者为空可删除

        List<CheckTaskAnomalyListVO> listVO = JsonUtil.getJsonToList(list, CheckTaskAnomalyListVO.class);
        for (CheckTaskAnomalyListVO checkTaskAnomalyVO : listVO) {
            checkTaskAnomalyVO.setParentid_name(checkTaskAnomalyVO.getParentid());
            checkTaskAnomalyVO.setWorkid_name(checkTaskAnomalyVO.getWorkid());
            checkTaskAnomalyVO.setConfirmtime_name(checkTaskAnomalyVO.getConfirmtime() != null ?
                    new Date(checkTaskAnomalyVO.getConfirmtime()) : null);
            checkTaskAnomalyVO.setConfirmuserid_name(generaterSwapUtil.userSelectValues(checkTaskAnomalyVO.getConfirmuserid()));
            checkTaskAnomalyVO.setConfirmdesc_name(checkTaskAnomalyVO.getConfirmdesc());

            CheckTaskEntity checkTaskEntity = checkTaskService.getInfo(checkTaskAnomalyVO.getParentid());
            checkTaskAnomalyVO.setCheckTaskEntity(checkTaskEntity);

            CheckTaskItemEntity checkTaskItemEntity = checkTaskItemService.getInfo(checkTaskAnomalyVO.getId());
            if (checkTaskItemEntity != null) {
                checkTaskItemEntity.setCreatoruserid(generaterSwapUtil.userSelectValue(checkTaskItemEntity.getCreatoruserid()));
                if (StringUtil.isNotEmpty(checkTaskItemEntity.getName())) {
//                    checkTaskItemEntity.setType(generaterSwapUtil.getDicName(checkTaskItemEntity.getType(), "529564962180304773"));
                    if ("1".equals(checkTaskItemEntity.getType())) {
                        checkTaskItemEntity.setName(equBaseService.getInfo(checkTaskItemEntity.getName()).getName());
                    } else {
                        Map<String, Object> checkPlanItemcheckidMap = new HashMap<>();
                        checkTaskItemEntity.setName(generaterSwapUtil.getPopupSelectValue("529327550036447045", "F_Id", "F_Name", (String) checkTaskItemEntity.getName(), checkPlanItemcheckidMap, "[]", 1, null));
                    }

                }            }

            checkTaskAnomalyVO.setCheckTaskItemEntity(checkTaskItemEntity);
            //子表数据转换
        }

        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = JsonUtil.getJsonToBean(checkTaskAnomalyPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);

    }

    /**
     * 创建
     *
     * @return
     */
    @PostMapping
    @Transactional
    @ApiOperation("创建")
    public ActionResult create(@RequestBody CheckTaskItemEntity entity) throws DataException {
        // 查询巡检项
        CheckTaskItemEntity itemEntity = checkTaskItemService.getInfo(entity.getId());
        if (itemEntity == null) {
            return ActionResult.fail("巡检项不存在");
        }
        String id = itemEntity.getId();
        CheckTaskAnomalyEntity info = checkTaskAnomalyService.getInfo(id);
        if (info != null) {
            return ActionResult.fail("已存在该异常");
        }
        // 异常项数据
        CheckTaskAnomalyEntity anomalyEntity = new CheckTaskAnomalyEntity();
        // 巡检项ID
        anomalyEntity.setId(id);
        // 巡检任务的ID
        anomalyEntity.setParentid(itemEntity.getParentid());
        checkTaskAnomalyService.save(anomalyEntity);

        return ActionResult.success("创建成功");
    }


    /**
     * 信息
     *
     * @param id
     * @return
     */
    @ApiOperation("信息")
    @GetMapping("/{id}")
    public ActionResult<CheckTaskAnomalyInfoVO> info(@PathVariable("id") String id) {
        CheckTaskAnomalyEntity entity = checkTaskAnomalyService.getInfo(id);
        CheckTaskAnomalyInfoVO vo = JsonUtil.getJsonToBean(entity, CheckTaskAnomalyInfoVO.class);

        //子表
        //副表
        return ActionResult.success(vo);
    }

    /**
     * 表单信息(详情页)
     *
     * @param id
     * @return
     */
    @ApiOperation("表单信息(详情页)")
    @GetMapping("/detail/{id}")
    public ActionResult<CheckTaskAnomalyInfoVO> detailInfo(@PathVariable("id") String id) {
        ActionResult info = info(id);
        Map<String, Object> dataMap = JsonUtil.entityToMap(info.getData());
        CheckTaskAnomalyEntity entity = checkTaskAnomalyService.getInfo(id);
        CheckTaskAnomalyInfoVO vo = JsonUtil.getJsonToBean(entity, CheckTaskAnomalyInfoVO.class);
        vo.setConfirmuserid(generaterSwapUtil.userSelectValues(vo.getConfirmuserid()));


        CheckTaskEntity checkTaskEntity = checkTaskService.getInfo(vo.getParentid());
        checkTaskEntity.setCheckteam(generaterSwapUtil.comSelectValues(checkTaskEntity.getCheckteam(), false));
        vo.setCheckTaskEntity(checkTaskEntity);

        CheckTaskItemEntity checkTaskItemEntity = checkTaskItemService.getInfo(vo.getId());
        vo.setCheckTaskItemEntity(checkTaskItemEntity);

        return ActionResult.success(vo);
    }


    /**
     * 更新
     *
     * @param id
     * @param checkTaskAnomalyForm
     * @return
     */
    @PutMapping("/{id}")
    @Transactional
    @ApiOperation("更新")
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid CheckTaskAnomalyForm checkTaskAnomalyForm) throws DataException {
        String b = checkTaskAnomalyService.checkForm(checkTaskAnomalyForm, 1);
        if (StringUtil.isNotEmpty(b)) {
            return ActionResult.fail(b + "不能重复");
        }
        UserInfo userInfo = userProvider.get();
        CheckTaskAnomalyEntity entity = checkTaskAnomalyService.getInfo(id);
        if (entity != null) {
            CheckTaskAnomalyEntity subentity = JsonUtil.getJsonToBean(checkTaskAnomalyForm, CheckTaskAnomalyEntity.class);
            boolean b1 = checkTaskAnomalyService.updateById(subentity);
            if (!b1) {
                return ActionResult.fail("当前表单原数据已被调整，请重新进入该页面编辑并提交数据");
            }
            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败，数据不存在");
        }
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        CheckTaskAnomalyEntity entity = checkTaskAnomalyService.getInfo(id);
        if (entity != null) {
            checkTaskAnomalyService.delete(entity);
        }
        return ActionResult.success("删除成功");
    }


}
